package com.ling.pl.security.view.global;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.ling.core.common.utils.D7PageUtils;
import com.ling.pl.core.context.ContextHolder;
import com.ling.pl.security.feign.DefaultPositionService;
import com.ling.pl.security.model.DefaultPositionVO;
import com.ling.pl.security.model.DefaultUserVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author bo.wang
 * @since 2013-2-17
 */
@Component("ling2.positionSelect")
public class PositionSelect {
    @Resource
    private DefaultPositionService positionService;

    @DataProvider
    public void loadPositions(Page<DefaultPositionVO> page, Criteria criteria) throws Exception {
        DefaultUserVO user = ContextHolder.getLoginUser();
        if (user == null) {
            throw new RuntimeException("Please login first");
        }
        String companyId = user.getCompanyId();
        Map params = D7PageUtils.buildQueryParams(criteria);
        params.put("companyId", companyId);
        D7PageUtils.daoPageToPage(positionService.findDefaultPositionByPage(D7PageUtils.buildQueryParams(params, page)), page);
    }
}
